package cn.csg.iotplatform.module.system.service.project;

import cn.csg.iotplatform.module.common.imports.ExcelImportException;
import cn.csg.iotplatform.module.common.imports.ExcelValidate;
import cn.csg.iotplatform.module.common.util.ExcelImportUtil;
import cn.csg.iotplatform.module.system.api.dept.DeptApi;
import cn.csg.iotplatform.module.system.dal.dataobject.dept.DeptDO;
import cn.csg.iotplatform.module.system.dal.dataobject.manufacturer.ManufacturerDO;
import cn.csg.iotplatform.module.system.service.dept.DeptService;
import cn.csg.iotplatform.module.system.service.manufacturer.ManufacturerService;
import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 项目导入校验逻辑
 */
@Slf4j
public class ProjectImportValidate extends ExcelValidate {

    private ProjectService projectService;
    private DeptService deptService;
    private ManufacturerService manufacturerService;

    public ProjectImportValidate() {
        this.projectService = SpringUtil.getBean(ProjectService.class);
        this.deptService = SpringUtil.getBean(DeptService.class);
        this.manufacturerService = SpringUtil.getBean(ManufacturerService.class);
    }

    @Override
    protected String validateAndFormatColumn(Integer rowIndex, Integer colIndex, String columnName, String value, Map<String, String> row) throws ExcelValidateException {
        switch (columnName){
            case "项目名称":
            case "项目编码":
            case "负责人":
            //case "承建单位项目负责人":
            //case "承建单位负责人电话":
                if(StrUtil.isBlank(value)) throw new ExcelValidateException("不能为空");
                break;
            case "项目单位":
                if(StrUtil.isBlank(value)) throw new ExcelValidateException("不能为空");
                DeptDO deptDO = deptService.getDeptByOrgCode(value);
                if(deptDO == null) throw new ExcelValidateException("项目单位不存在");
                break;
            case "承建单位":
                if(StrUtil.isNotBlank(value)){
                    ManufacturerDO manufacturerDO = manufacturerService.getDeviceManufacturerByNameAndType(value,"2");
                    if(manufacturerDO == null) throw new ExcelValidateException("承建单位不存在");
                }
                break;
            case "允许接入网关设备数":
            case "允许接入子设备数":
            case "允许接入普通设备数":
                if(StrUtil.isBlank(value)) throw new ExcelValidateException("不能为空");
                if(!ReUtil.isMatch(RegexPool.NUMBERS,value)) throw new ExcelValidateException("必须为数字类型");
                break;
            default:
                break;
        }
        return value == null? "" : value;
    }

    @Override
    protected void validateHead(Map<Integer, String> heard) throws ExcelImportException {
        ExcelImportUtil.checkHeaderByEntity(ProjectImportValidate.class, heard.values());
    }
}
